<?php
/**
 * 日志管理控制器
 * @author Moon
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class LogController extends WahahabaseController{
	
	private $log_model;
	
	public function _initialize() {
		$this->log_model = M('Log');
	}

	public function index(){
		$work_date	= time();
		$begin		= $eventBegin	= mktime(0,0,0,date('m'),1,date('Y'));
		$end		= $eventEnd		= mktime(23,59,59,date('m'),date('t'),date('Y'));
		if(IS_POST){
			$begin	= $eventBegin	= $work_date = strtotime(I('post.work_date').'-1');
			$end	= $eventEnd		= mktime(23,59,59,date('m',$begin),date('t',$begin),date('Y',$begin));
		}
		if(I('get.time')){
			$day_time	=  $work_date = strtotime(I('get.time')); 
			$begin		= mktime(0,0,0,date('m',$day_time),date('d',$day_time),date('Y',$day_time));
			$end		= $begin + 24*60*60 -1;
		}
		$daysNum 	= date('t',$work_date);
		$month		= date('m',$work_date);
		$year		= date('Y',$work_date);
		$condition['is_delete']	= 0;
		$condition['is_show']	= 1;
		$condition['addtime']	= array('between',array($begin,$end));
		$count		= $this->log_model->where($condition)->count();
        $page		= $this->page($count, 20);
		$results	= $this->log_model->where($condition)
					->order("addtime DESC")
					->limit($page->firstRow, $page->listRows)
					->select();
		$eventCondition['is_delete']	= 0;
		$eventCondition['is_show']		= 1;
		$eventCondition['addtime']		= array('between',array($eventBegin,$eventEnd));
		$events = $this->log_model->field("FROM_UNIXTIME(addtime,'%Y-%m-%d') day")
					->where($eventCondition)
					->group('day')
					->select();	
		$eventDays = '|';
		foreach($events as $day){
			$event_day = explode('-',$day['day']);
			$eventDays .= (int)$event_day[2].'|';
		}
		$role_id = D("Common/Users")->where('id='.session('ADMIN_ID'))->getField('role_id');
		$this->assign("role_id",$role_id);
		$this->assign(array('daysNum'=>$daysNum,'year'=>$year,'month'=>$month,'eventDays'=>$eventDays,'results'=>$results,'page'=>$page->show('Admin')));
		$this->display();
	}
	
	public function add(){
		$this->display();
	}
	
	public function add_post(){
		if (IS_POST) {
            $data				= I('post.');
			$data['uid']		= session('ADMIN_ID');
			$data['username']	= session('name');
			$data['partment']	= M('Department')->where("id=%d",session('deptid'))->getField('department_name');
			$data['addtime']	= time();

            if ($this->log_model->create()) {
                if ($this->log_model->add($data)) {
                    $this->success("添加成功！", U("Log/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->log_model->getError());
            }
        }
	}
	
	public function view($id){
		$result = $this->log_model->where("id=%d",$id)->find();
		$this->assign(array('result'=>$result));
		$this->display();
	}
	
	public function edit($id){
		$result = $this->log_model->where("id=%d",$id)->find();
		$this->assign(array('result'=>$result));
		$this->display();
	}
	
	public function edit_post(){
		if (IS_POST) {
			$data					= I('post.');
            $condition['id']		= $data['id'];
			$condition['uid']		= session('ADMIN_ID');
			unset($data['id']);
			$saveData				= $data;
			$saveData['addtime']	= time();

			if ($this->log_model->where($condition)->save($data)) {
				$this->success("编辑成功！", U("Log/index"));
			} else {
				$this->error("添加失败！");
			}
        }
	}
	
	public function delete($id){
		if ($this->log_model->where("id=%d",$id)->delete()) {
			$this->success("删除成功！", U("Log/index"));
		} else {
			$this->error("删除失败！");
		}
	}



}